﻿using OfficeOpenXml;
using OfficeOpenXml.Style;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ExcelFileQueryTool
{
    public partial class frmDialyFile : Form
    {
        public frmDialyFile()
        {
            InitializeComponent();
        }

        private void DispatchInfoMessage(string content)
        {
            if (string.IsNullOrEmpty(content))
                throw new ArgumentNullException("content");

            MessageDispatcherManager.DispatchMessage(new AppMessage
            {
                Level = AppMessageLevel.Info,
                Time = DateTime.Now,
                Title = "日记文件添加新表操作",
                Content = content
            });
        }

        private void btnCancle_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.Close();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            var file = txtFile.Text.Trim();

            if (string.IsNullOrEmpty(file))
            {
                DispatchInfoMessage("请选择日记文件！");
                return;
            }

            var begin = dtpBegin.Value;
            var end = dtpEnd.Value;

            if (begin > end)
            {
                DispatchInfoMessage("起始日期不能大于截至日期！");
                return;
            }

            if (begin.Month != end.Month)
            {
                DispatchInfoMessage("起始日期和截至日期必须在同一月份！");
                return;
            }

            var dayBegin = begin.Day;
            var dayEnd = end.Day;

            using (ExcelPackage p = new ExcelPackage(new FileInfo(file)))
            {
                for (int i = dayBegin; i <= dayEnd; i++)
                {
                    var name = begin.Month + "-" + i;
                    if (p.Workbook.Worksheets.Any(x => x.Name == name))
                        continue;

                    var sheet = p.Workbook.Worksheets.Add(name, p.Workbook.Worksheets[1]);
                    sheet.Cells[1, 1].Value = string.Format("{0}年{1}月{2}日收支汇总", begin.Year, begin.Month, i);
                    p.Workbook.Worksheets.MoveToStart(sheet.Index);

                    for (int row = 5; row <= sheet.Dimension.End.Row; row++)
                    {
                        sheet.Cells[row, 1].Value = string.Format("{0}-{1}-{2}", begin.Year, begin.Month, i);
                        sheet.Cells[row, 2].Value = "销售";
                        sheet.Cells[row, 3].Value = string.Empty;
                        sheet.Cells[row, 3].Style.Fill.PatternType = ExcelFillStyle.Solid;
                        sheet.Cells[row, 3].Style.Fill.BackgroundColor.SetColor(Color.White);
                        sheet.Cells[row, 4].Value = string.Empty;
                        sheet.Cells[row, 5].Value = string.Empty;
                        sheet.Cells[row, 6].Clear();
                        sheet.Cells[row, 7].Clear();
                        sheet.Cells[row, 8].Value = 0d;
                        sheet.Cells[row, 10].Value = string.Empty;
                        sheet.Cells[row, 11].Value = string.Empty;
                        sheet.Cells[row, 12].Clear();
                        sheet.Cells[row, 13].Value = string.Empty;
                    }
                }

                p.Save();
            }

            DispatchInfoMessage("添加成功！");

            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            this.Close();
        }

        private void txtFile_DoubleClick(object sender, EventArgs e)
        {
            using (OpenFileDialog diag = new OpenFileDialog())
            {
                diag.Filter =AppConfig.ClientFileChooseFilter;

                if (diag.ShowDialog(this) == DialogResult.OK)
                    txtFile.Text = diag.FileName;
            }
        }
    }
}
